package com.example.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.beans.PaperInformation;
import com.example.myapplication.AnswerActivity;
import com.example.myapplication.R;
import com.example.myapplication.UserInfoActivity;

import com.github.siyamed.shapeimageview.CircularImageView;
import com.lidroid.xutils.BitmapUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/3/17.
 */
public class PaperInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<PaperInformation> list = new ArrayList<>();
    private int TYPE_INFO = 0;
    private int TYPE_FOOT = 1;
    private BitmapUtils bitmapUtils;
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_INFO){
            View view  = inflater.inflate(R.layout.list_paper_info,parent,false);
            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
            ViewContentHodler viewContentHodler = new ViewContentHodler(view,onClick);
            bitmapUtils = new BitmapUtils(context);
            return viewContentHodler;


        }else if(viewType ==TYPE_FOOT){

            View view = inflater.inflate(R.layout.list_workinfo_footer,null);
            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
            FootsHolder footsHolder = new FootsHolder(view);
            return footsHolder;
        }
        return null;
    }
    public void changeList(List<PaperInformation> list){

        this.list = list;
        notifyDataSetChanged();
    }

    OnItemClick onClick = new OnItemClick() {
        @Override
        public void OnItemClick(PaperInformation paperInformation, int position) {

            Intent intent = new Intent();
            intent.setClass(context, UserInfoActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("userinfo", paperInformation);
            intent.putExtras(bundle);
            context.startActivity(intent);
        }

        @Override
        public void OnStudentClick(PaperInformation paperInformation, int position) {

            Intent intent = new Intent();
            intent.setClass(context, AnswerActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("userinfo", paperInformation);
            intent.putExtras(bundle);
            context.startActivity(intent);

        }
    };

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ViewContentHodler){
            ((ViewContentHodler)holder).tvContent.setText(list.get(position).getPaper_context());
            ((ViewContentHodler) holder).tvDate.setText(list.get(position).getPaper_time());
            ((ViewContentHodler) holder).tvStudentName.setText(list.get(position).getStudent_name());
            ((ViewContentHodler) holder).tvStudentNumber.setText(list.get(position).getStudent_number());
            ((ViewContentHodler) holder).tvTitle.setText(list.get(position).getPaper_name());
            Log.i("pic",list.get(position).getStudent_pic());
            String student_pic="";
            char a = list.get(position).getStudent_pic().charAt(0);
            char b = list.get(position).getStudent_pic().charAt(1);
            if(a=='.'){
                student_pic = list.get(position).getStudent_pic().substring(1);
            }
            if(b=='.'){
                student_pic = list.get(position).getStudent_pic().substring(2);
            }
            bitmapUtils.display( ((ViewContentHodler)holder).ivStudent, "http://192.168.191.1/app/pic" +student_pic);
            ((ViewContentHodler) holder).bind(list.get(position));
        }else if(holder instanceof FootsHolder){

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    @Override
    public int getItemViewType(int position){
            if(list.size()>0) {
                return list.get(position).getType();
            }
            return 0;
        }


    public PaperInfoAdapter(Context context,List<PaperInformation> list){
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.list  =list;

    }
    public class ViewContentHodler extends RecyclerView.ViewHolder implements View.OnClickListener{
        private LinearLayout liner_info,liner_student;
        private TextView tvStudentName,tvStudentNumber,tvContent,tvTitle,tvDate;
        private CircularImageView ivStudent;
        private OnItemClick onitemListener;
        private CardView cardView;
        public ViewContentHodler(View itemView, OnItemClick mListener) {
            super(itemView);
            this.onitemListener = mListener;
            cardView = (CardView) itemView.findViewById(R.id.cardview);
            liner_info = (LinearLayout) itemView.findViewById(R.id.linear_info);
            liner_student = (LinearLayout) itemView.findViewById(R.id.user_info);
            tvStudentName = (TextView) itemView.findViewById(R.id.tv_studnet_name);
            tvStudentNumber = (TextView) itemView.findViewById(R.id.tv_number);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            tvDate = (TextView) itemView.findViewById(R.id.tv_time);
            ivStudent = (CircularImageView) itemView.findViewById(R.id.Iv_student);
            cardView.setOnClickListener(this);
            tvStudentName.setOnClickListener(this);
            tvStudentNumber.setOnClickListener(this);
            ivStudent.setOnClickListener(this);

        }

        public void bind(PaperInformation paperInformation){
            cardView.setTag(paperInformation);
            tvStudentNumber.setTag(paperInformation);
            tvStudentName.setTag(paperInformation);
            ivStudent.setTag(paperInformation);


        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.tv_studnet_name:
                    onitemListener.OnItemClick((PaperInformation) cardView.getTag(),getLayoutPosition());
                    break;
                case R.id.tv_number:
                    onitemListener.OnItemClick((PaperInformation) cardView.getTag(),getLayoutPosition());
                    break;
                case R.id.Iv_student:
                    onitemListener.OnItemClick((PaperInformation) cardView.getTag(),getLayoutPosition());
                    break;
                case R.id.cardview:
                    onitemListener.OnStudentClick((PaperInformation) cardView.getTag(),getLayoutPosition());
            }
        }
    }

    public interface OnItemClick{
        void OnItemClick(PaperInformation workInfoemation, int position);
        void OnStudentClick(PaperInformation workInfoemation,int position);
    }
    public static class FootsHolder extends  RecyclerView.ViewHolder {


        public FootsHolder(View itemView) {
            super(itemView);

        }
    }




}
